﻿using System;
using System.Web.UI;
using Pantry.Business;
using Pantry.Commons;

namespace Pantry.Web.Users.Controls
{
    //Register a delegate
    public delegate void SendValuesToThePageHandler(string valuesToThePage);
    public partial class ListTypeOfBusiness : System.Web.UI.UserControl
    {
        public event SendValuesToThePageHandler SendvaluesToThePage;
        protected void Page_Load(object sender, EventArgs e)
        {
            Fillter(textboxBusiness.Text,TextboxSIC.Text);
        }

        protected void Fillter(string name,string sicCode)
        {
            var dt =  new TypeOfBusinessBus().ListTypeOfBusinessToDataSet().Tables[0];
            var dataView = dt.DefaultView;
            dataView.RowFilter = "TypeName LIKE '%"+name+"%' AND SicCode LIKE '%"+sicCode+"%'";

            CollectionPager.PageSize = ConstantSetup.ContactPageSize;
            CollectionPager.BindToControl = RepeaterBusiness;
            CollectionPager.DataSource = dataView;
            RepeaterBusiness.DataSource = CollectionPager.DataSourcePaged;
            RepeaterBusiness.DataBind();
        }

        protected void Search_Click(object sender, EventArgs e)
        {
            //Response.Redirect("ViewListTypeOfBusiness.aspx?TypeName=" + textboxBusiness.Text.Trim() + "&Code=" + TextboxSIC.Text.Trim() + "");
            Fillter(textboxBusiness.Text, TextboxSIC.Text);
        }
        protected void None_Click(object sender, EventArgs e)
        {
            textboxBusiness.Text = null;
            TextboxSIC.Text = null;
            Fillter(textboxBusiness.Text, TextboxSIC.Text);
        }
        protected void Clear_Click(object sender, EventArgs e)
        {
            textboxBusiness.Text = null;
            TextboxSIC.Text = null;
            
        }

        protected void ButtonSelectClick(object sender,EventArgs e)
        {
            if (!string.IsNullOrEmpty(HttpHelper.GetFormString("RadioTypeId")))
            {
                var typeOfBusiness = HttpHelper.GetFormString("RadioTypeId");
                if (SendvaluesToThePage != null)
                {
                    SendvaluesToThePage(typeOfBusiness);
                }
                ScriptManager.RegisterStartupScript(UpdatePanel1, GetType(), "Key", "ClosePopup()", true);
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, GetType(), "YourUniqueScriptKey", "alert('Please select one row !');", true);
            }
        }
    }
}